package cn.iocoder.yudao.module.erp.service.factorycalendar;

import com.xingyuv.captcha.util.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.erp.controller.admin.factorycalendar.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.factorycalendar.FactoryCalendarDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.erp.dal.mysql.factorycalendar.FactoryCalendarMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*;

/**
 * ERP工厂日历 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FactoryCalendarServiceImpl implements FactoryCalendarService {

    @Resource
    private FactoryCalendarMapper factoryCalendarMapper;

    @Override
    public Long createFactoryCalendar(FactoryCalendarSaveReqVO createReqVO) {
        // 插入
        FactoryCalendarDO factoryCalendar = BeanUtils.toBean(createReqVO, FactoryCalendarDO.class);
        factoryCalendarMapper.insert(factoryCalendar);
        // 返回
        return factoryCalendar.getFid();
    }

    @Override
    public void updateFactoryCalendar(FactoryCalendarSaveReqVO updateReqVO) {
        // 校验存在
        validateFactoryCalendarExists(updateReqVO.getFid());
        // 更新
        FactoryCalendarDO updateObj = BeanUtils.toBean(updateReqVO, FactoryCalendarDO.class);
        factoryCalendarMapper.updateById(updateObj);
    }

    @Override
    public void deleteFactoryCalendar(Long id) {
        // 校验存在
        validateFactoryCalendarExists(id);
        // 删除
        factoryCalendarMapper.deleteById(id);
    }

    private void validateFactoryCalendarExists(Long id) {
        if (factoryCalendarMapper.selectById(id) == null) {
            throw exception(FACTORY_CALENDAR_NOT_EXISTS);
        }
    }

    @Override
    public List<FactoryCalendarDO> getFactoryCalendar(String fyear) {
        return factoryCalendarMapper.selectList(FactoryCalendarDO::getFyear, fyear);
    }

    @Override
    public PageResult<FactoryCalendarDO> getFactoryCalendarPage(FactoryCalendarPageReqVO pageReqVO) {
        return factoryCalendarMapper.selectPage(pageReqVO);
    }

    @Override
    public List<Long> createFactoryCalendars(List<FactoryCalendarSaveReqVO> createReqVOList) {

        // 如果createReqVOList为空，直接返回空列表
        if (createReqVOList.isEmpty()) {
            return Collections.emptyList();
        }
        // 插入前根据fyear删除对应年份的数据
        String fyear = createReqVOList.get(0).getFyear();
        factoryCalendarMapper.delete(FactoryCalendarDO::getFyear, fyear);

        if(createReqVOList.size() == 1) {
            FactoryCalendarDO factoryCalendarDO = BeanUtils.toBean(createReqVOList.get(0), FactoryCalendarDO.class);
            if(StringUtils.isEmpty(factoryCalendarDO.getFdate())) {
                // 如果fdate为空，直接返回空列表
                return Collections.emptyList();
            }
        }

        List<FactoryCalendarDO> factoryCalendarDOList = BeanUtils.toBean(createReqVOList, FactoryCalendarDO.class);
        // 过滤掉fdate为空的数据
        factoryCalendarDOList = factoryCalendarDOList.stream()
                .filter(factoryCalendarDO -> StringUtils.isNotEmpty(factoryCalendarDO.getFdate()))
                .collect(Collectors.toList());
        factoryCalendarMapper.insertBatch(factoryCalendarDOList);
        return factoryCalendarDOList.stream().map(FactoryCalendarDO::getFid).collect(Collectors.toList());
    }

}